package com.chaofan.sell.entity.mapper;

import com.chaofan.sell.entity.ProductCategory;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Fairy
 * @date 2021/11/4
 */
@Repository
public interface ProductCategoryMapper {

    /**
     * 插入分类
     *
     * @param map 类别map
     * @return 受影响的行数
     */
    @Insert("insert into product_category values (#{categroy_name, jdbcType=VARCHAR}, #{category_type, jdbcType=INTEGER})")
    int insertByMap(Map<String, Object> map);

    /**
     * 通过类型查询
     *
     * @param categoryType 类型编号
     * @return ProductCategory
     */
    @Select("select * from product_category where category_type = #{categoryType}")
    @Results({
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "category_type", property = "categoryType")
    })
    ProductCategory findByType(Integer categoryType);

    /**
     * 通过类型名查询
     *
     * @param categoryName 类型名
     * @return 允许重名，return List
     */
    @Select("select * from product_category where category_name = #{categoryName}")
    @Results({
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "category_type", property = "categoryType")
    })
    List<ProductCategory> findByName(String categoryName);

    /**
     * 通过Type编号改名
     *
     * @param categoryName target name
     * @param categoryType 编号
     * @return int
     */
    @Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType}")
    int updateByCategoryType(@Param("categoryName") String categoryName, @Param("categoryType") Integer categoryType);

    /**
     * 通过对象更新
     *
     * @param productCategory 待更新的对象
     * @return int
     */
    @Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType}")
    int updateByObject(ProductCategory productCategory);

    /**
     * 通过type删除
     *
     * @param categoryType 待删除的type
     * @return int
     */
    @Delete("delete from product_category where category_type=#{categoryType};")
    int deleteByType(Integer categoryType);

    /**
     * 通过id查找
     *
     * @param categoryId 要查找的id
     * @return {ProductCategory}
     */
    ProductCategory selectById(Integer categoryId);
}
